package de.szut.traineeslife.taks;

import java.io.File;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import de.szut.traineeslife.adapter.FilesFromDriveAdapter;
import de.szut.traineeslife.adapter.FilesFromSDAdapter;
import de.szut.traineeslife.utils.DialogListener;
import de.szut.traineeslife.utils.FileHelper;

public class LoadDocumentsTask extends AsyncTask<Void, Void, Void> {

	private ProgressDialog dialog;
	private Context mContext;
	private FileHelper mFileHelper;
	private ArrayList<File> mFileList = new ArrayList<File>();
	private String mPath;
	private DialogListener mDialogListener;

	/**
	 * Checks the Filepath of documents for a specific ending and dispaly them
	 * in a listview
	 * 
	 * @param context
	 * @param path
	 * @param fileHelper
	 * @param listener
	 */
	public LoadDocumentsTask(Context context, String path, FileHelper fileHelper, DialogListener listener) {
		mContext = context;
		mPath = path;
		mFileHelper = fileHelper;
		mDialogListener = listener;
	}

	@Override
	protected Void doInBackground(Void... params) {
		mFileList.clear();
		try {
			mPath = mDialogListener.getSelectedModelFolderPath();
			for (String s : mFileHelper.loadDirectory(mPath)) {
				File f = new File(s);
				if (!mFileList.contains(f)) {
					mFileList.add(f);
				}
			}
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		if (mFileList != null) {
			mDialogListener.startSelectModelDialog(mFileList);
		}
		if (dialog != null)
			dialog.dismiss();
	}

	@Override
	protected void onPreExecute() {
		Log.d("PRE", Integer.toString(mFileList.size()));
		super.onPreExecute();
		dialog = new ProgressDialog(mContext);
		dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dialog.setMessage("Durchsucht Pfad....");
		dialog.setCancelable(false);
		dialog.show();
	}

}
